package com.csoa.community.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Calendar;
import java.util.Date;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;

public class AndroidUtils {
	public static View inflateView(Context context, int resource, ViewGroup root) {
		return View.inflate(context, resource, root);
	}

	public static String getSdcardPath() {
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	public static byte[] drawable2ByteArray(Drawable _d, CompressFormat format) {
		return null == _d ? Bitmap2Bytes(drawable2Bitmap(_d), format) : null;
	}

	public static byte[] Bitmap2Bytes(Bitmap _b, CompressFormat format) {
		if (null == _b) {
			return null;
		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		_b.compress(format, 100, baos);
		return baos.toByteArray();
	}

	/**
	 * 上传图片质量的方法
	 * 
	 * @param _b
	 * @param format
	 * @param isPost
	 * @return
	 */
	public static byte[] Bitmap2Bytes(Bitmap _b, CompressFormat format,
			boolean isPost) {
		if (null == _b) {
			return null;
		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		_b.compress(format, 100, baos);
		return baos.toByteArray();
	}

	public static Bitmap drawable2Bitmap(Drawable _d) {
		return null == _d ? null : ((BitmapDrawable) _d).getBitmap();
	}

	public static Bitmap byteArray2Bitmap(byte[] _b) {
		return null == _b ? null : BitmapFactory.decodeByteArray(_b, 0,
				_b.length);
	}

	public static Drawable resource2Drawable(Activity _activity, int id) {
		return null == _activity ? null : bitmap2Drawable(BitmapFactory
				.decodeResource(_activity.getResources(), id));
	}

	public static Drawable byteArray2Drawable(byte[] _b) {
		return null == _b ? null : bitmap2Drawable(BitmapFactory
				.decodeByteArray(_b, 0, _b.length));
	}

	public static Drawable bitmap2Drawable(Bitmap bitmap) {
		if (bitmap == null)
			return null;
		if (160 != bitmap.getDensity()) {
			bitmap.setDensity(160);
		}
		return new BitmapDrawable(bitmap);
	}

	/*
	 * 打开网页
	 */
	public static void openUriInBrowser(Activity _activity, String url) {
		if (null == _activity || StringUtils.isEmpty(url)) {
			return;
		}
		Uri uri = Uri.parse(url);
		_activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
	}

	public static boolean fileInvoke(Context activity, String path) {
		String mimeType = getMimeType(path);
		System.out.println("file path:" + path + ",mimeType:" + mimeType);
		if (!StringUtils.isEmpty(mimeType)) {
			Intent it = new Intent(Intent.ACTION_VIEW);
			Uri uri = null;
			if (mimeType.startsWith("application/")
					|| mimeType.startsWith("image/")) {
				it.addCategory("android.intent.category.DEFAULT");
				uri = Uri.fromFile(new File(path));
				it.setDataAndType(uri, mimeType);
				it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				activity.startActivity(it);
				return true;
			} else if (mimeType.startsWith("audio/")
					|| mimeType.startsWith("video/")
					|| mimeType.startsWith("text/")) {
				uri = Uri.parse("file://" + path);
				it.setDataAndType(uri, mimeType);
				activity.startActivity(it);
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	public static String getMimeType(String path) {
		String ext = getFileExtensionFromUrl(path);
		if (StringUtils.isEmpty(ext)) {
			return "";
		}
		return MimeTypeMap.getSingleton().getMimeTypeFromExtension(
				ext.toLowerCase());
	}

	public static String getFileExtensionFromUrl(String url) {
		if (url != null && url.length() > 0) {
			int query = url.lastIndexOf('?');
			if (query > 0) {
				url = url.substring(0, query);
			}
			int filenamePos = url.lastIndexOf('/');
			String filename = 0 <= filenamePos ? url.substring(filenamePos + 1)
					: url;

			if (filename.length() > 0) {
				int dotPos = filename.lastIndexOf('.');
				if (0 <= dotPos) {
					return filename.substring(dotPos + 1);
				}
			}
		}

		return "";
	}

	/**
	 * 去除字符串最后逗号
	 * 
	 * @return
	 */
	public static String replaceTheEndComma(String str) {
		if(StringUtils.isEmpty(str)) {
			return "";
		}
		
		String lastAlpha = str.substring(str.length() - 1, str.length());
		
		if(!lastAlpha.equals(",")) {
			return str;
		}
		String strNew = str.substring(0, str.lastIndexOf(","));
		
		return strNew;
	}
	

	/**
	 * 判断网络是否可用
	 * 
	 * @return
	 */
	public static boolean isNetworkValidate(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() != null) {
			return cm.getActiveNetworkInfo().isAvailable();
		}
		return false;
	}

	public static void showDialog(final Context c, String title,
			final String[] datas, DialogInterface.OnClickListener listener) {
		final String[] items;
		if (datas != null) {
			String[] temp = new String[datas.length + 1];
			System.arraycopy(datas, 0, temp, 0, datas.length);
			temp[temp.length - 1] = "取消";
			items = temp;
		} else {
			items = new String[] { "取消" };
		}
		new AlertDialog.Builder(c).setTitle(title).setItems(items, listener)
				.create().show();
	}
	
	
	public static int getSampleSize(String path)
	{
		BitmapFactory.Options bmfoptions=new BitmapFactory.Options(); 
		bmfoptions.inSampleSize = 1;
		bmfoptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path,bmfoptions);
		int wr = bmfoptions.outWidth/100;
		int hr = bmfoptions.outHeight /100;
		if(wr>hr){
			return wr;
		}else{
			return hr;
		}
	}
	
	public static BitmapFactory.Options getBitmapOptions(String path) {
		BitmapFactory.Options options=new BitmapFactory.Options(); 
		options.inPreferredConfig = Bitmap.Config.RGB_565;
		options.inSampleSize = getSampleSize(path);
		return options;
	}


	/**
	 * 存放全局变量
	 * 
	 * @param key
	 * @param value
	 * @param context
	 */
	public static void setGlobal(String type, String key, String value,
			Context context) {
		Editor sharedata = context.getSharedPreferences(type, 0).edit();
		sharedata.putString(key, value);
		sharedata.commit();
	}

	/**
	 * 取全局变量
	 * 
	 * @param key
	 * @param context
	 * @return
	 */
	public static String getGlobal(String type, String key, Context context) {
		SharedPreferences sharedata = context.getSharedPreferences(type, 0);
		return sharedata.getString(key, "");
	}

	/**
	 * 移除全局变量
	 * 
	 * @param type
	 * @param key
	 * @param context
	 * @return
	 */
	public static void removeGlobal(String type, String key, Context context) {
		Editor sharedata = context.getSharedPreferences(type, 0).edit();
		sharedata.remove(key);
		sharedata.commit();
	}

	public static int getSelectDataToNow(long data) {
		try {
			Date date2 = Calendar.getInstance().getTime();
			return (int) ((date2.getTime() - data) / 1000 / 60 / 60 / 24);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 调节屏幕亮度
	 */
	public static void adjustScreenBright(Activity context,
			int screentBrightValue) {
		WindowManager.LayoutParams lp = context.getWindow().getAttributes();
		if (screentBrightValue >= 0f && screentBrightValue <= 255f) {
			float value = screentBrightValue / 255f;
			lp.screenBrightness = value;
		} else {
			lp.screenBrightness = -1.0f;
		}
		context.getWindow().setAttributes(lp);
	}

	public static void adjustScreenBright(Activity context) {
		// adjustScreenBright(context,GlobalVars.screenBrightValue);
	}

}
